package com.wzt.lianfirecontrol.adapter.xiaokongshi;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.utils.Utils;

public class XksPersonAdapter extends BaseRecyclerAdapter<UserInfoModel> {

    private Context context;

    private boolean isDetail = false;

    private PersonCallBack callBack;

    public XksPersonAdapter(Context context) {
        this.context = context;
    }

    public boolean isDetail() {
        return isDetail;
    }

    public void setDetail(boolean detail) {
        isDetail = detail;
    }

    public PersonCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(PersonCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_xks_person, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, UserInfoModel data) {
        if (viewHolder instanceof MyViewHolder){
            setMyData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_zhiban_person;
        private TextView tv_person_name;
        private TextView tv_person_phone;
        private ImageView iv_del_person;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initMyView(this, itemView);
        }
    }

    private void initMyView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_zhiban_person = itemView.findViewById(R.id.tv_zhiban_person);
        myViewHolder.tv_person_name = itemView.findViewById(R.id.tv_person_name);
        myViewHolder.tv_person_phone = itemView.findViewById(R.id.tv_person_phone);
        myViewHolder.iv_del_person = itemView.findViewById(R.id.iv_del_person);
        if (isDetail){
            myViewHolder.iv_del_person.setVisibility(View.GONE);
            myViewHolder.tv_zhiban_person.setVisibility(View.VISIBLE);
        }
    }

    private void setMyData(MyViewHolder viewHolder, UserInfoModel data, int realPosition) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (isDetail){
            layoutParams.setMargins(0, 0, 0, Utils.dip2px(context, 0.5f));
        }else {
            layoutParams.setMargins(0, Utils.dip2px(context, 0.5f), 0, 0);
        }
        viewHolder.tv_person_name.setText(data.getRealName());
        viewHolder.tv_person_phone.setText(data.getPhone());
        viewHolder.iv_del_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.deletePerson(data);
            }
        });
    }

    public interface PersonCallBack{
        void deletePerson(UserInfoModel userInfoModel);
    }

}
